<template>
  <!-- 搜索组件 -->
  <div id="main">
    <div id="hotcontent">
      <div class="lll">
        <h2>
          搜索：
          <font color="red">“{{ search_keys }}”</font>
          相关的小说
        </h2>
        <!-- 搜索结果表格 -->
        <!-- 用户列表区域  table -->
        <el-table
          :data="Searchlist"
          border
          stripe
          :header-cell-style="{ textAlign: 'center' }"
          :cell-style="{ textAlign: 'center' }"
        >
          <el-table-column type="index" label="ID"></el-table-column>
          <el-table-column label="类别" prop="book_cate" width="80">
            <template slot-scope="scope">
              <div @click="JumpCatesRoute(scope.row.book_cate)">
                {{ scope.row.book_cate }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="书名" prop="book_name" width="300">
            <template slot-scope="scope">
              <div @click="JumpDetailRoute(scope.row.book_id)">
                {{ scope.row.book_name }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="最新章节" prop="book_newest_name" width="300">
            <template slot-scope="scope">
              <div @click="JumpChapterRoute(scope.row.book_id,scope.row.book_newest_url)">
                {{ scope.row.book_newest_name }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="书籍情况" prop="book_status" width="120">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.book_status === '连载中'"
                >连载中</el-tag
              >
              <el-tag type="warning" v-else>完本</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="作者" prop="book_author"></el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
import '../../assets/css/search.css'
export default {
  data() {
    return {
      // 搜索的关键词
      search_keys: '',
      // 搜索的小说数据列表
      Searchlist: [],
      // 分类小说路由
      books_cate_route: ''
    }
  },
  created() {
    // 获取路由传递过来的 搜索key
    this.search_keys = this.$route.params.key
    // console.log(this.search_keys)

    // 获取搜索接口数据
    this.getSearchKeylist()
  },
  methods: {
    // 点击事件，跳转小说详情页
    JumpDetailRoute(book_id) {
      this.$router.push({
        path: '/book/' + book_id
      })
    },
    // 点击事件跳转分类小说
    JumpCatesRoute(url) {
      this.$router.push({
        path: this.$store.state.fiction_cate[url]
      })
    },
    // 点击事件，跳转小说章节页
    JumpChapterRoute(book_id, sort_id) {
      this.$router.push({
        path: '/chapter/' + book_id + '/' + sort_id
      })
    },
    // 获取搜索接口数据
    async getSearchKeylist() {
      const { data: res } = await this.$http.get(
        `search_infos/${this.search_keys}`
      )

      if (res.status !== 200) {
        return this.$message.error('请求搜索数据失败！')
      }

      // 搜素结果小说列表数据变量
      this.Searchlist = res.data
      // console.log(this.Searchlist)

      // 分类小说路由
      this.books_cate_route = this.$store.state.fiction_cate[
        this.Searchlist.book_cate
      ]
    }
  }
}
</script>

<style lang="less" scoped></style>
